/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tal.drivechain.module.xmlconfig;

import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 *
 * @author Main
 */
public class PackageConfiguration {
    private PackageInfo packageInfo;
    private HashMap<String,ModuleInfo> modules = new HashMap<String,ModuleInfo>();
    private HashMap<String, LoggerInfo> loggers = new HashMap<String,LoggerInfo>();
    
    public PackageConfiguration(URI packagefile) {
        try {
            DOMParser parser = new DOMParser();
            InputSource src = new InputSource(packagefile.toString());
            parser.parse(src);
            Document configDoc = parser.getDocument();
            packageInfo = new PackageInfo(configDoc, packagefile);
            NodeList moduleNodes = configDoc.getElementsByTagName("module");
            for (int i=0; i<moduleNodes.getLength(); i++) {
                String name = moduleNodes.item(i).getAttributes().getNamedItem("name").getNodeValue().trim();
                try {
                    modules.put(name, new ModuleInfo(moduleNodes.item(i), packageInfo, this));
                } catch (IllegalArgumentException ie) {
                    throw new IllegalArgumentException("Error encountered while reading from XML at " + ie.getMessage());
                }
            }
            
            NodeList loggerNodes = configDoc.getElementsByTagName("controller");
            for (int i=0; i<loggerNodes.getLength(); i++) {
                String name = loggerNodes.item(i).getAttributes().getNamedItem("name").getNodeValue().trim();
                loggers.put(name, new LoggerInfo(loggerNodes.item(i)));
            }
            
        } catch (SAXException ex) {
            Logger.getLogger(PackageConfiguration.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PackageConfiguration.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    }
    
    public PackageInfo getPackageInfo() { return packageInfo; }
    public HashMap<String,ModuleInfo> getModulesInfo() { return modules; }
    public HashMap<String,LoggerInfo> getLoggersInfo() { return loggers; }
}
